<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>QeePHP 快速入门</title>
<link href="css/base.css" rel="stylesheet" type="text/css">
</head>
<body>

<div id="page">


<div class="guide-section">

  <div class="guide-header">
    <span class="nav">
      <a href="http://qee13.com/app/?action=docs">文档索引</a>
      &raquo;
      <a href="index.html">QeePHP 快速入门</a>
      &raquo;
      <a href="node-development.html">开始开发</a>
      &raquo;
      创建模型    </span>
  </div>

  <div class="guide-section-details formatted">
    
<h1>创建模型</h1>

<p>每个模型对应数据库里面的一张数据表。</p>

<h2>创建数据表</h2>

<p>在 phpMyAdmin 中选中先前创建的 todo_db 数据，然后执行下列
SQL 命令：</p>

<pre class="sql code"><span class="kw1">CREATE</span> <span
class="kw1">TABLE</span> <span class="st0">`todo_db`</span><span
class="sy0">.</span><span class="st0">`tests`</span> <span
class="br0">&#40;</span>
  <span class="st0">`id`</span> INT <span
class="kw1">NOT</span> <span class="kw1">NULL</span> <span
class="kw1">AUTO_INCREMENT</span> <span class="kw1">PRIMARY</span> <span
class="kw1">KEY</span> <span class="sy0">,</span>
  <span
class="st0">`title`</span> VARCHAR<span class="br0">&#40;</span> <span
class="nu0">80</span> <span class="br0">&#41;</span> <span
class="kw1">NOT</span> <span class="kw1">NULL</span> <span
class="sy0">,</span>
  <span class="st0">`body`</span> TEXT <span
class="kw1">NOT</span> <span class="kw1">NULL</span> <span
class="sy0">,</span>
  <span class="st0">`created`</span> INT <span
class="kw1">NOT</span> <span class="kw1">NULL</span> <span
class="sy0">,</span>
  <span class="st0">`updated`</span> INT <span
class="kw1">NOT</span> <span class="kw1">NULL</span>
<span
class="br0">&#41;</span>;</pre>

<p>执行成功后，数据库将多出一个 tests 表。</p>

<h2>从数据表创建模型</h2>

<p>新建文件 _code/model/tests.php</p>

<p>加入如下内容：</p>

<pre class="php code"><span
class="co4">/**
 * Tests 封装来自 tests 数据表的记录及领域逻辑
 */</span>
<span
class="kw2">class</span> Tests <span
class="kw2">extends</span> QDB_ActiveRecord_Abstract
<span
class="br0">&#123;</span>
&nbsp;
    <span
class="co4">/**
     * 返回对象的定义
     *
     * @static
     *
     * @return array
     */</span>
    <span
class="kw3">static</span> <span class="kw2">function</span> __define<span
class="br0">&#40;</span><span class="br0">&#41;</span>
    <span
class="br0">&#123;</span>
        <span class="kw1">return</span> <span
class="kw3">array</span>
        <span
class="br0">&#40;</span>
&nbsp;
            <span
class="co1">// 用什么数据表保存对象</span>
            <span
class="st_h">'table_name'</span> <span class="sy0">=&gt;</span> <span
class="st_h">'tests'</span><span class="sy0">,</span>
        <span
class="br0">&#41;</span><span class="sy0">;</span>
     <span
class="br0">&#125;</span>
     <span
class="co4">/**
     * 开启一个查询，查找符合条件的对象或对象集合
     *
     * @static
     *
     * @return QDB_Select
     */</span>
    <span
class="kw3">static</span> <span class="kw2">function</span> find<span
class="br0">&#40;</span><span class="br0">&#41;</span>
    <span
class="br0">&#123;</span>
        <span class="re0">$args</span> <span
class="sy0">=</span> <span class="kw3">func_get_args</span><span
class="br0">&#40;</span><span class="br0">&#41;</span><span
class="sy0">;</span>
        <span
class="kw1">return</span> QDB_ActiveRecord_Meta<span class="sy0">::</span><span
class="me2">instance</span><span class="br0">&#40;</span><span
class="kw2">__CLASS__</span><span class="br0">&#41;</span><span
class="sy0">-&gt;</span><span class="me1">findByArgs</span><span
class="br0">&#40;</span><span class="re0">$args</span><span
class="br0">&#41;</span><span class="sy0">;</span>
    <span
class="br0">&#125;</span>
&nbsp;
    <span
class="co4">/**
     * 返回当前 ActiveRecord 类的元数据对象
     *
     * @static
     *
     * @return QDB_ActiveRecord_Meta
     */</span>
    <span
class="kw3">static</span> <span class="kw2">function</span> meta<span
class="br0">&#40;</span><span class="br0">&#41;</span>
    <span
class="br0">&#123;</span>
        <span
class="kw1">return</span> QDB_ActiveRecord_Meta<span class="sy0">::</span><span
class="me2">instance</span><span class="br0">&#40;</span><span
class="kw2">__CLASS__</span><span class="br0">&#41;</span><span
class="sy0">;</span>
    <span class="br0">&#125;</span>
<span
class="br0">&#125;</span></pre>

<p>此外，这个文件中已经定义好了三个重要的静态方法（static
function）。其中，<strong>__define()</strong>
返回的内容指出了保存模型使用的数据表、属性设置以及模型的其他方面。以后我们对模型的许多修改都集中在
__define() 方法中。</p>

<blockquote>
	<p>这个模型是用于测试的，不需要时删除掉即可。对应的
	tests 数据表也可以删除。</p>
</blockquote>
$Id: development-model.texy 2013-08-14 02:24:25Z Tim13 $
  </div>

  <div class="guide-footer">

    <table border="0" width="100%">
      <tr>
        <td align="left" width="200">
                    &laquo;
          <a href="node-development-controller.html">创建控制器</a>
          
        </td>

        <td align="center">
          本章：<a href="node-development.html">开始开发</a>
          <br />
          <a href="index.html">返回索引页</a>
        </td>

        <td align="right" width="200">
                    <a href="node-thinking.html">编写代码之前的思考</a> 
          &raquo;
                  </td>
      </tr>
    </table>

  </div>

</div>


</div>

</body>
</html>


